﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_DAO.db;
using Cashier_Model;
using System.Data.SQLite;
using Cashier_DAO;
using System.Data;
using Cashier_Utils;
using System.Data.Common;

namespace Cashier_Service
{
    public class AdminService
    {
        /// <summary>
        /// 获取管理员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Admin GetAdmin(long id)
        {
            Admin admin = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    admin = new Admin();
                    admin.Id = id;
                    admin = new AdminDAO().GetAdmin(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return admin;
        }

        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Admin Login(String username, String password)
        {
            Admin admin = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    admin = new Admin();
                    admin.Username = username;
                    admin.Password = EncryptionUtil.Md5(password);
                    admin = new AdminDAO().GetAdmin(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return admin;
        }

        /// <summary>
        /// 获取管理员列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="storeNo"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public DataTable GetAdminTable(int pageindex, int pagesize, String enterpriseNo, String storeNo, long roleid)
        {
            DataTable dt = null;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Admin admin = new Admin();
                    admin.Pageindex = pageindex;
                    admin.Pagesize = pagesize;
                    admin.EnterpriseNo = enterpriseNo;
                    admin.StoreNo = storeNo;
                    admin.Roleid = roleid;
                    dt = new AdminDAO().GetAdminTable(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        /// <summary>
        /// 获取管理员记录数
        /// </summary>
        /// <param name="storeNo"></param>
        /// <param name="enterpriseNo"></param>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public long GetAdminRecord(String enterpriseNo, String storeNo, long roleid)
        {
            long record = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Admin admin = new Admin();
                    admin.EnterpriseNo = enterpriseNo;
                    admin.StoreNo = storeNo;
                    admin.Roleid = roleid;
                    record = new AdminDAO().GetAdminRecord(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return record;
        }

        /// <summary>
        /// 新增管理员
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public long InsertAdmin(Admin admin)
        {
            long id = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    id = new AdminDAO().InsertAdmin(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return id;
        }

        /// <summary>
        /// 更新管理员信息
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public int UpdateAdmin(Admin admin)
        {
            int flag = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    flag = new AdminDAO().UpdateAdmin(conn, admin);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        /// <summary>
        /// 删除管理员
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DeleteAdmin(String ids)
        {
            int count = 0;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    if (!String.IsNullOrEmpty(ids))
                    {
                        conn.Open();
                        String[] arr = ids.Split(new Char[] { ',' });
                        AdminDAO adminDAO = new AdminDAO();
                        Admin admin = new Admin();
                        foreach (String lopid in arr)
                        {
                            admin.Id = Int64.Parse(lopid);
                            count += adminDAO.DeleteAdmin(conn, admin);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return count;
        }

        /// <summary>
        /// 判断是否存在相同的用户名
        /// </summary>
        /// <param name="id"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool ExistSameUsername(long id, String username)
        {
            bool b = false;
            try
            {
                using (DbConnection conn = ConnectionFactory.GetDbConnection())
                {
                    conn.Open();
                    Admin admin = new Admin();
                    admin.Username = username;
                    DataTable dt = new AdminDAO().GetAdminTable(conn, admin);
                    foreach (DataRow row in dt.Rows)
                    {
                        long temp_id = (long)row["id"];
                        if (id != temp_id)
                        {
                            b = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return b;
        }
    }
}
